package com.soo.hoo.admin.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.soo.hoo.admin.mapper.SysUserRoleMapper;
import com.soo.hoo.admin.model.entity.SysUserRole;
import com.soo.hoo.admin.service.UserRoleService;
import com.soo.hoo.core.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * <p>用户角色关联服务</p>
 *
 * @author 杨燚
 * @since 2023/4/23
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class UserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUserRole> implements UserRoleService {

    @Override
    public void saveRelevance(Long userId, List<Long> roleIds) {
        if (CollUtil.isEmpty(roleIds)){
            return;
        }
        List<SysUserRole> userRoles = new ArrayList<>();
        for (Long roleId : roleIds) {
            SysUserRole userRole = new SysUserRole();
            userRole.setUserId(userId);
            userRole.setRoleId(roleId);
            userRoles.add(userRole);
        }
        baseMapper.insertBatchSomeColumn(userRoles);
    }

    @Override
    public void deleteByUserId(Long userId) {
        LambdaQueryWrapper<SysUserRole> lqw = new LambdaQueryWrapper<SysUserRole>()
                .eq(SysUserRole::getUserId, userId);
        baseMapper.delete(lqw);
    }

    @Override
    public List<SysUserRole> listByUserIds(Collection<?> userIds) {
        LambdaQueryWrapper<SysUserRole> lqw = new LambdaQueryWrapper<SysUserRole>()
                .in(SysUserRole::getUserId, userIds);
        return baseMapper.selectList(lqw);
    }
}
